package org.dey.serviceImpl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.dey.entity.Result;
import org.dey.mapper.InterventionTemplateMapper;

import org.dey.pojo.Exercise;
import org.dey.pojo.InterventionTemplate;
import org.dey.pojo.PageVo;
import org.dey.service.InterventionTemplateService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author hg
 * @date 2022/4/5
 * @classname
 */
@Service
@Transactional
public class InterventionTemplateServiceImpl extends ServiceImpl<InterventionTemplateMapper, InterventionTemplate> implements InterventionTemplateService {

    @Override
    public PageVo<InterventionTemplate> setmealXx(InterventionTemplate interventionTemplate, int pageNum, int pagesize) {

        QueryWrapper<InterventionTemplate> queryWrapper = new QueryWrapper<InterventionTemplate>();

        queryWrapper.eq("condi",1);

        if(StringUtils.isNotBlank(interventionTemplate.getName())){

            queryWrapper.like("name",interventionTemplate.getName());
        }

        PageHelper.startPage(pageNum, pagesize);

        List<InterventionTemplate> list = this.list(queryWrapper);

        PageInfo<InterventionTemplate> pageInfo = new PageInfo<InterventionTemplate>(list);


        PageVo<InterventionTemplate> pageVo = new PageVo<InterventionTemplate>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return  pageVo;
    }


    @Override
    public Result templateXxsc(int id) {

        Result result = null;

        UpdateWrapper<InterventionTemplate> updateWrapper = new UpdateWrapper<InterventionTemplate>();
        updateWrapper.eq("id",id).setSql("condi=0");

        boolean update = this.update(updateWrapper);

        if(update){
            result = new Result(true,"干预模版删除成功");
        }else{
            result = new Result(true,"干预模版删失败");
        }

        return result;
    }

    @Override
    public Result addtemplateXx(InterventionTemplate interventionTemplate) {
        Result result = null;

        String str1 = "";
        String str2 = "";

        //先拿到运动项目id 组装成字符串
        for (int i = 0; i < interventionTemplate.getExercises().length; i++) {

            if(interventionTemplate.getExercises().length-1==i){
             str1 = str1 + interventionTemplate.getExercises()[i].getId();
            }else{
                str1 = str1 + interventionTemplate.getExercises()[i].getId()+",";
            }
        }
        interventionTemplate.setExerciseid(str1);
        //先拿到膳食项目id 组装成字符串
        for (int i = 0; i < interventionTemplate.getDietaries().length; i++) {

            if(interventionTemplate.getDietaries().length-1==i){
                str2 = str2 + interventionTemplate.getDietaries()[i].getId();
            }else{
                str2 = str2 + interventionTemplate.getDietaries()[i].getId()+",";
            }
        }
        interventionTemplate.setDietaryid(str2);
        interventionTemplate.setCondi(1);

        boolean save = this.save(interventionTemplate);

        if(save){
            result = new Result(true,"添加模版成功");
        }else{
            result = new Result(true,"添加模版失败");
        }

        return result;
    }

    @Override
    public InterventionTemplate querytemplateXxbyid(int id) {

        InterventionTemplate byId = this.getById(id);

        //获取到运动id
        String exerciseid = byId.getExerciseid();
        String[] strings1 = exerciseid.split(",");
        int[] number1 = new int[strings1.length];
        for(int i =0;i<strings1.length;i++){
            number1[i] = Integer.parseInt(strings1[i]);
        }
        byId.setExe(number1);


        //获取到膳食id
        String dietaryid = byId.getDietaryid();
        String[] strings2 = dietaryid.split(",");
        int[] number2 = new int[strings2.length];
        for(int i =0;i<strings2.length;i++){
            number2[i] = Integer.parseInt(strings2[i]);
        }
        byId.setDie(number2);


        return byId;
    }

    @Override
    public Result updatetemplateXx(InterventionTemplate interventionTemplate) {

        Result result = null;

        String str1 = "";
        String str2 = "";

        //先拿到运动项目id 组装成字符串
        for (int i = 0; i < interventionTemplate.getExercises().length; i++) {

            if(interventionTemplate.getExercises().length-1==i){
                str1 = str1 + interventionTemplate.getExercises()[i].getId();
            }else{
                str1 = str1 + interventionTemplate.getExercises()[i].getId()+",";
            }
        }

        //先拿到膳食项目id 组装成字符串
        for (int i = 0; i < interventionTemplate.getDietaries().length; i++) {

            if(interventionTemplate.getDietaries().length-1==i){
                str2 = str2 + interventionTemplate.getDietaries()[i].getId();
            }else{
                str2 = str2 + interventionTemplate.getDietaries()[i].getId()+",";
            }
        }



        UpdateWrapper<InterventionTemplate> updateWrapper = new UpdateWrapper<InterventionTemplate>();
        updateWrapper.eq("id",interventionTemplate.getId()).set("exerciseid",str1).set("dietaryid",str2);
        boolean update = this.update(updateWrapper);

        if(update){
            result = new Result(true,"模版修改成功");
        }else{
            result = new Result(true,"模版修改失败");
        }
        return result;
    }

    @Override
    public List<InterventionTemplate> mobanXx() {

        List<InterventionTemplate> list = this.list();

        return list;
    }
}
